<template>
    <div class="app-container">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="120px"
      >
      <el-form-item label="仓库" prop="warehouseId">
          <el-select
            v-model="queryParams.warehouseId"
            style="width: 215px"
            placeholder="请选择仓库"
            filterable 
            remote
            @focus="searchWarehouse()"
            :remote-method="warehouseOptions"
            clearable
            size="small"
          >
            <el-option
              v-for="item in warehouseOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
      </el-form-item>
        <el-form-item label="平台" prop="platform">
          <el-input
            v-model="queryParams.platform"
            placeholder="请输入平台"
            clearable
            size="small"
            style="width: 215px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="平台海关编码" prop="salesFilingCode">
          <el-input
            v-model="queryParams.salesFilingCode"
            placeholder="请输入平台海关编码"
            clearable
            size="small"
            style="width: 215px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            v-hasPermi="['datahub:warehouse_platform:add']"
            @click="handleAdd"
            >新增</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @handleQuery="handleQuery"
          @resetQuery="resetQuery"
        ></right-toolbar>
      </el-row>
  
      <el-table
        v-loading="loading"
        :data="warehouseList"
        v-adaptive="{ bottomOffset: 50 }"
        height="100px"
      >
        <el-table-column
          label="仓库名称"
          min-width="150"
          align="center"
          prop="warehouseName"
        >
        </el-table-column>
        <el-table-column
          label="仓库编码"
          min-width="150"
          align="center"
          prop="warehouseCode"
        >
        </el-table-column>
        <el-table-column
          label="平台"
          min-width="150"
          align="center"
          prop="platform"
          show-overflow-tooltip
        />
        <el-table-column
          label="平台编码"
          min-width="150"
          align="center"
          prop="platformCode"
          show-overflow-tooltip
        />
        <el-table-column
          label="平台海关编码"
          min-width="150"
          align="center"
          prop="salesFilingCode"
          show-overflow-tooltip
        />
        <el-table-column
          label="操作"
          width="100"
          align="center"
          fixed="right"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['datahub:warehouse_platform:edit']"
              style="margin-right: 10px"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="handleDeletel(scope.row)"
              v-hasPermi="['datahub:warehouse_platform:remove']"
              style="margin-right: 10px"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加或修改商品分类对话框 -->
      <el-dialog
        :title="title"
        :visible.sync="open"
        width="640px"
        append-to-body
        @close="closeDialog"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row>
            <el-col :span="16">
              <el-form-item
                label="仓库"
                prop="warehouseId"
                :rules="{
                  required: true,
                  message: '请选择仓库',
                  trigger: 'blur',
                }"
              >
              <el-select
              v-model="form.warehouseId"
              style="width: 250px"
              placeholder="请选择仓库"
              filterable
              remote
              @change="warehouseChange"
              @focus="searchWarehouse()"
              :remote-method="warehouseOptions"
              clearable
              size="small"
            >
              <el-option
                v-for="item in warehouseOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <!--  -->
            <el-col :span="16">
              <el-form-item
                label="平台"
                prop="platform"
                :rules="{
                  required: true,
                  message: '请输入平台',
                  trigger: 'blur',
                }"
              >
              <el-input
                  style="width: 250px"
                  v-model="form.platform"
                  placeholder="请输入平台"
                />
                <!-- <el-select
                  v-model="form.platform"
                  style="width: 250px"
                  placeholder="请选择平台"
                  clearable
                  filterable
                  size="small"
                >
                  <el-option
                    v-for="dict in platformList"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictLabel"
                  />
                </el-select> -->
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="16">
              <el-form-item
                label="平台编码"
                prop="platformCode"
                :rules="{
                  required: true,
                  message: '请输入平台编码',
                  trigger: 'blur',
                }"
              >
                <el-input
                  style="width: 250px"
                  v-model="form.platformCode"
                  placeholder="请输入平台编码"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="16">
              <el-form-item
                label="平台海关编码"
                prop="salesFilingCode"
              >
                <el-input
                  style="width: 250px"
                  v-model="form.salesFilingCode"
                  placeholder="请输入平台海关编码"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="open = false">取 消</el-button>
        </div>
      </el-dialog>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <div v-if="activate"></div>
    </div>
  </template>
  
  <script>
  import {
    warehousePlatform,
    addWarehousePlatform,
    updateWarehousePlatform,
    getDeletel,
  } from "@/api/common/warehouse";
  
  import {
    listWarehouse
  } from "@/api/stock/warehouse";
  export default {
    name: "Label",
    components: {},
    data() {
      return {
        open: false,
        title: "",
        activate: true,
        // 遮罩层
        loading: false,
        form: {},
        // 显示搜索条件
        showSearch: false,
        // 总条数
        total: 0,
        // 表格数据
        warehouseList: [],
        //菜单名称
        platformList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        //仓库
        warehouseOptions:[]
      };
    },
    created() {
      // 物流公司名称
      this.getDicts("sys_platform_type").then((res) => {
        this.platformList = res.data;
      });
      this.getList();
      this.searchWarehouse();
    },
    //触发回流
    activated() {
      this.activate = false;
      setTimeout(() => {
        this.activate = true;
      });
      this.getList();
    },
    methods: {
      /** 查询商品信息列表 */
      getList() {
        this.loading = true;
        warehousePlatform(this.queryParams).then((response) => {
          this.warehouseList = response.data;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams = {
          pageNum: 1,
          pageSize: 10,
        };
        this.handleQuery();
      },
      //获取仓库列表
      searchWarehouse(){
        listWarehouse({status:0}).then(res=>{
          this.warehouseOptions = res.data;
        })
      },
      warehouseChange(v){
        this.warehouseOptions.forEach(item=>{
          if(v==item.id){
            this.form.warehouseName = item.name;
            this.form.warehouseCode = item.code;
          }
        })
      },
      submitForm() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (this.form.id != null) {
              updateWarehousePlatform(this.form).then((response) => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addWarehousePlatform(this.form).then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      //弹框关闭事件
      closeDialog() {
        //清空表单校验
        this.$refs.form.resetFields();
        this.form = {};
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.title = "仓库平台映射新增";
        this.open = true;
      },
  
      /** 修改按钮操作 */
      handleUpdate(row) {
          console.log(this.form,row);
          this.form = {...row};
          this.title = "仓库平台映射修改";
          this.open = true;
      },
      handleDeletel(row) {
        this.$confirm(
          "是否确认删除物流公司名称为 “" + row.logisticsCompany + "” 的数据项？",
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        ).then(() => {
          getDeletel(row.id).then((res) => {
            this.getList();
            this.msgSuccess("操作成功");
          });
        });
      },
    },
  };
  </script>
  <style lang="scss" scoped>
  .treeselect-main {
    width: 193px;
  }
  </style>
  